﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using DevExpress.Web.Mvc;

namespace KimPhongCRM.Controllers
{
    [PermissionFilter]
    public class LichSuTourController : BaseController
    {
        private Entities db = new Entities();
        List<KimPhongCRM.Models.sp_Select_Combobox_User_Result> listUser = new List<sp_Select_Combobox_User_Result>();

        public LichSuTourController()
        {
            listUser = db.sp_Select_Combobox_User().ToList();
        }
        //
        // GET: /LichSuTour/
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Lịch sử", Key = "lsTour", Clickable = true, ParentKey = "Tour")]
        public ViewResult Index()
        {
            ViewData["listUser"] = listUser;
            return View(db.tblLichSuTours.ToList());
        }

        //
        // GET: /LichSuTour/Details/5

        public ViewResult Details(long id)
        {
            tblLichSuTour tbllichsutour = db.tblLichSuTours.Find(id);
            return View(tbllichsutour);
        }

        //
        // GET: /LichSuTour/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /LichSuTour/Create

        [HttpPost]
        public ActionResult Create(tblLichSuTour tbllichsutour)
        {
            if (ModelState.IsValid)
            {
                db.tblLichSuTours.Add(tbllichsutour);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tbllichsutour);
        }

        //
        // GET: /LichSuTour/Edit/5

        public ActionResult Edit(long id)
        {
            tblLichSuTour tbllichsutour = db.tblLichSuTours.Find(id);
            return View(tbllichsutour);
        }

        //
        // POST: /LichSuTour/Edit/5

        [HttpPost]
        public ActionResult Edit(tblLichSuTour tbllichsutour)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tbllichsutour).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tbllichsutour);
        }

        //
        // GET: /LichSuTour/Delete/5

        public ActionResult Delete(long id)
        {
            tblLichSuTour tbllichsutour = db.tblLichSuTours.Find(id);
            return View(tbllichsutour);
        }

        //
        // POST: /LichSuTour/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            tblLichSuTour tbllichsutour = db.tblLichSuTours.Find(id);
            db.tblLichSuTours.Remove(tbllichsutour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        KimPhongCRM.Models.Entities db1 = new KimPhongCRM.Models.Entities();

        [ValidateInput(false)]
        public ActionResult LichSuTourPartial()
        {
            ViewData["listUser"] = listUser;
            var model = db1.tblLichSuTours;
            return PartialView("_LichSuTourPartial", model.ToList());
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult LichSuTourPartialAddNew(KimPhongCRM.Models.tblLichSuTour item)
        {
            var model = db1.tblLichSuTours;
            if (ModelState.IsValid)
            {
                try
                {
                    model.Add(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_LichSuTourPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult LichSuTourPartialUpdate(KimPhongCRM.Models.tblLichSuTour item)
        {
            var model = db1.tblLichSuTours;
            if (ModelState.IsValid)
            {
                try
                {
                    var modelItem = model.FirstOrDefault(it => it.maLichSu == item.maLichSu);
                    if (modelItem != null)
                    {
                        this.UpdateModel(modelItem);
                        db1.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_LichSuTourPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult LichSuTourPartialDelete(System.Int64 maLichSu)
        {
            var model = db1.tblLichSuTours;
            if (maLichSu != null)
            {
                try
                {
                    var item = model.FirstOrDefault(it => it.maLichSu == maLichSu);
                    if (item != null)
                        model.Remove(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_LichSuTourPartial", model.ToList());
        }
    }
}